<template>
  <section class="page-content">
    <div class="toolbar">
      <el-form :inline="true" :model="params">
        <el-form-item>
          <el-button type="success" size="small" icon="plus" @click="create">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      v-loading="loading"
      :data="tableData.data"
      @sort-change="sort"
      border
      style="width: 100%"
      max-height="100%">
      <el-table-column
        prop="id"
        sortable
        width="80"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="name"
        width="120"
        label="课程名称">
      </el-table-column>
      <el-table-column
        width="120"
        label="缩略图">
        <template slot-scope="scope">
          <img :src="base_server_url + '/storage/' + scope.row.thumb" style="width:100px; margin-top:10px;" alt="">
        </template>
      </el-table-column>
      <el-table-column
        prop="teacher_id"
        sortable
        label="教师">
        <template slot-scope="scope">
          {{scope.row.user.name}}
        </template>
      </el-table-column>
      <el-table-column
        prop="category_id"
        sortable
        label="课程分类">
        <template slot-scope="scope">
          {{scope.row.category_name}}
        </template>
      </el-table-column>
      <el-table-column
        label="课程属性">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.is_best">精品推荐</el-tag>
          <el-tag type="success" v-if="scope.row.is_required">必看课程</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        sortable
        prop="is_on_sale"
        label="是否上架">
        <template slot-scope="scope">
          <i class="el-icon-check" v-if="scope.row.is_on_sale"></i>
          <i class="el-icon-close" v-if="!scope.row.is_on_sale"></i>
        </template>
      </el-table-column>
      <el-table-column
        prop="price"
        sortable
        label="课程售价">
      </el-table-column>
      <el-table-column
        prop="order"
        sortable
        label="课程排序">
      </el-table-column>
      <el-table-column
        fixed="right"
        width="80"
        label="操作">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="章节列表" placement="top-start">
            <i class="el-icon-tickets" @click="$router.push('/courses/catalog/' + scope.row.id)"></i>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="编辑" placement="top-start">
            <i class="el-icon-edit-outline" @click="editRow(scope.row)"></i>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top-start">
            <i @click="deleteRow(scope.row)" class="el-icon-delete"></i>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <div class="block toolbar">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="tableData.current_page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="parseInt(tableData.per_page)"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total">
      </el-pagination>
    </div>
  </section>
</template>
<script>
  import {generateUrl} from '@/utils/helpers'
  import {BASE_SERVER_URL} from '@/utils/config'
  export default {
    data () {
      return {
        loading: false,
        base_server_url: BASE_SERVER_URL,
        tableData: {
          first_page_url: '',
          from: '',
          to: '',
          last_page: '',
          last_page_url: '',
          next_page_url: '',
          prev_page_url: '',
          path: '',
          per_page: 0,
          total: 0,
          data: []
        },
        searchData: {
          name: ''
        },
        params: {
          order_by: 'order',
          order_type: 'asc',
          page_size: 20,
          platform: 'backend',
          name: '',
          page: 1
        }
      }
    },
    mounted () {
      this._getData()
    },
    watch: {
      params: {
        deep: true,
        handler (newValue) {
          this._getData()
        }
      }
    },
    methods: {
      deleteRow (row) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http.delete('/courses/' + row.id).then((response) => {
            this.loading = false
            this.$message.success('删除成功')
            this._getData()
          }).catch((error) => {
            this.loading = false
            console.log(error)
          })
        })
      },
      editRow (item) {
        this.$router.push('/courses/' + item.id + '/edit')
      },
      onSubmit () {
        this.params.name = this.searchData.name
      },
      handleSizeChange (val) {
        this.params.page_size = val
      },
      handleCurrentChange (val) {
        this.params.page = val
      },
      sort (data) {
        let orderType = ''
        if (data.order === 'ascending') {
          orderType = 'asc'
        }
        if (data.order === 'descending') {
          orderType = 'desc'
        }
        this.params.order_type = orderType
        this.params.order_by = data.prop
      },
      create () {
        this.$router.push('/courses/create')
      },
      _getData () {
        let url = generateUrl('/courses', this.params)
        this.loading = true
        this.$http.get(url).then((response) => {
          this.loading = false
          this.tableData = response.data
        }).catch((error) => {
          this.loading = false
          console.log(error)
        })
      }
    }
  }
</script>
<style lang="scss" scoped>
  .dialog {
    .el-select {
      width: 80%;
    }
    .el-input {
      width: 80%;
    }
    .el-textarea {
      width: 80%;
    }
  } 
</style>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #20a0ff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 220px;
    height: 140px;
    line-height: 140px;
    text-align: center;
  }
  .upload-thumb {
    width: 220px;
    height: 140px;
    display: block;
  }
</style>
